﻿using System;
using System.Windows.Forms;
using System.IO;
using System.Net;

namespace Gui.Functies.Filesharing
{
    public partial class Download : Form
    {
        private int projectID;
        private string selected;
        private string ftp = "ftp://ftp.qq-design.com";
        private string ftpfilepath;
        private string usn = "alex";
        private string pass = "Huibenalexmoetengoedophunwachtwoordletten";

        public Download()
        {

        }

        public Download(int projectnummer, string selectedfile)
        {
            projectID = projectnummer;
            selected = selectedfile;
            ftpfilepath = ftp+'/'+projectID+'/'+selected;
            InitializeComponent();
            textBoxFile.Text = selected;
        }

        private void btnDownload_Click(object sender, EventArgs e)
        {
            //request aanmaken
            WebClient request = new WebClient();

            //credentials setten
            request.Credentials = new NetworkCredential(usn, pass);

            //data in bytearray zetten
            byte[] data = request.DownloadData(ftpfilepath);

            //filestream aanmaken waar de bytearray naartoe geschreven word
            FileStream streamfile = File.Create(textBoxDestination.Text+"\\"+selected);

            //array naar file schrijven
            streamfile.Write(data, 0, data.Length);

            //file sluiten en gebruiker informeren dat het gelukt is
            streamfile.Close();

            this.Dispose();
            
            MessageBox.Show("het bestand is gedownload");
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderDlg = new FolderBrowserDialog();
            folderDlg.ShowNewFolderButton = true;

            // Show the FolderBrowserDialog
            DialogResult result = folderDlg.ShowDialog();

            if (result == DialogResult.OK)
            {
                textBoxDestination.Text = folderDlg.SelectedPath;
                Environment.SpecialFolder root = folderDlg.RootFolder;
            }
        }
    }
}
